package com.yst.gateway.monitor.services.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yst.gateway.monitor.common.utils.DateUtils;
import com.yst.gateway.monitor.common.utils.LoginUserCache;
import com.yst.gateway.monitor.common.utils.UUIDGenerator;
import com.yst.gateway.monitor.dao.QzBusinessMapper;
import com.yst.gateway.monitor.models.QzBusiness;
import com.yst.gateway.monitor.models.QzChannelBusiness;
import com.yst.gateway.monitor.services.QzBusinessService;


/**
 * 前置系统字典管理实现类
 * 
 * @author Administrator
 *
 */
@Service
@Scope("prototype")
public class QzBusinessServiceImpl implements QzBusinessService {

	@Autowired
	private QzBusinessMapper qzBusinessMapper;
	
	
	
	@Override
	public PageInfo<QzBusiness> getByPage(int pageNum, int pageSize,
			Map<String, Object> paramMap) {
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<QzBusiness>(qzBusinessMapper.selectAll(paramMap));
	}

	@Override
	public boolean add(QzBusiness t) {
		boolean flag = true;
		t.setBusinessId(UUIDGenerator.getUUID());
		t.setCtime(DateUtils.getNowTime());
		t.setCreator(LoginUserCache.getUser().getSysUserName());
		t.setStatus(2);
		if (qzBusinessMapper.insert(t) > 0) {
			
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean update(QzBusiness t) {
		boolean flag = true;
		t.setUtime(DateUtils.getNowTime());
		t.setModifier(LoginUserCache.getUser().getSysUserName());
		if (qzBusinessMapper.updateByPrimaryKey(t) > 0) {
			 
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public QzBusiness getById(String id) {
		return qzBusinessMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<QzBusiness> getByMap(Map<String, Object> paramMap) {
		return qzBusinessMapper.selectAll(paramMap);
	}


	@Override
	public boolean delById(String id) {
		boolean flag = true;
		if (qzBusinessMapper.deleteByPrimaryKey(id) > 0) {
			
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean updateStatusById(String id) {
		boolean flag = true;
		QzBusiness business = getById(id);
		if (business != null) {
			business.setStatus(0);
			business.setUtime(DateUtils.getNowTime());
			business.setModifier(LoginUserCache.getUser().getSysUserName());
		}
		List<QzChannelBusiness> list = selectByBusinessId(id);
		if (list != null && list.size() > 0) {
			flag = false;
		} else {
			qzBusinessMapper.updateByPrimaryKey(business);
			
		}
		return flag;
	}

	@Override
	public List<QzBusiness> selectByName(String name) {
		return qzBusinessMapper.selectByName(name);
	}

	@Override
	public List<QzChannelBusiness> selectByBusinessId(String businessId) {
		return qzBusinessMapper.selectByBusinessId(businessId);
	}

	@Override
	public List<QzBusiness> getByChannelId(String channelId) {
		
		return qzBusinessMapper.getByChannelId(channelId);
	}
}
